<?php
	/*
	 * Created by Nicky
	 *
	*/
	 /* permission name 
	 akademik_gdp_jadwalpelajaran
	 akademik_gdp_jadwalpelajaran_add
	 akademik_gdp_jadwalpelajaran_edit
	 akademik_gdp_jadwalpelajaran_del	 
	 */
	class Jadwal_pelajaranController extends CController{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Master Akademik";
		public $subMenu = "";
		public $title = "";		
		public function init() {
			if (!LoginUser::isLogin("akademik")){
				//$this->redirect(array('login/akademik','error'=>Yii::t("label", "Username atau password salah")));
				LoginUser::redirectToLoginPage("akademik");				
			}
						
		}
		
		public function actionIndex(){
			LoginUser::getPermission("akademik_gdp_jadwalpelajaran");
			$this->subMenu = "JadwalPelajaran";
			$this->layout ="//layouts/ly_blank";
			$data=array("department_id"=>"");			
			$this->render('index',$data);
		}
/*		
		public function actiondetailList(){		
			$onlyActive = Yii::app()->request->getParam("activeOnly",false);
			$department_id = Yii::app()->request->getParam("department_id","1");
			$karyawan_id = Yii::app()->request->getParam("karyawan_id","1");		
			$mcon= Yii::app()->db;
			$sql1='select * from m_jam_pelajaran left OUTER JOIN m_jadwal_pelajaran on m_jam_pelajaran.id = m_jadwal_pelajaran.pelajaran_ke ';
			$mJadwalPelajarancom=$mcon->createCommand($sql1);
			$mJadwalPelajarancom->bindValue('department_id',$department_id);
			$mJadwalPelajarancom->bindValue('karyawan_id',$karyawan_id);
			 
			$mJadwalPelajaran=$mJadwalPelajarancom->queryAll();	
 	
 	$this->result["actionStatus"] =0;
			$this->result["actionMessage"] ="";
			$this->result["actionError"] =array();
			$this->result["model"] =  $mJadwalPelajaran;			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
			 
			 
		}
	*/
		
		public function actionedit(){		
 
			$op = Yii::app()->request->getParam("op","add");
 			$status = Yii::app()->request->getParam("is_active",0) === "on"?1:0 ;			
			if ($op == "add"){
				LoginUser::getPermission("akademik_gdp_jadwalpelajaran_add");
				//print_r ($_POST);
				//echo "DATA SUDAH DI ADD........";
				$kelas_tahun_ajaran_id= Yii::app()->request->getParam("kelas_tahun_ajaran_id","");	
				$tahun_ajaran_id= Yii::app()->request->getParam("tahun_ajaran_id","");	
				$pelajaranKe=  Yii::app()->request->getParam("pelajaran_ke","");
				$pelajaranSampai= Yii::app()->request->getParam("pelajaran_sampai","");		
				$hari=Yii::app()->request->getParam("hari","");	
				$pelajaran_id=Yii::app()->request->getParam("pelajaran_id","");	
				$mcon= Yii::app()->db;
				$sql1='select A.id as id,F.tahun_ajaran_id,F.kelas_id,A.karyawan_id,A.pelajaran_id,A.status,A.hari,A.pelajaran_ke,A.pelajaran_sampai,A.is_active,A.keterangan, C.name as kelas_name, D.name as pelajaran_name , E.name as karyawan_name ,if (A.pelajaran_id='.$pelajaran_id.',0,1) as count_status ,(select name from m_jam_pelajaran where id=A.pelajaran_ke) as pelajaran_ke_name ,(select name from m_jam_pelajaran where id=A.pelajaran_sampai) as pelajaran_sampai_name from m_jadwal_pelajaran A left join m_kelas_tahun_ajaran F on A.kelas_tahun_ajaran_id=F.id left join m_kelas C on F.kelas_id=C.id left join m_pelajaran D on A.pelajaran_id=D.id left join m_karyawan E on A.karyawan_id=E.id   where F.tahun_ajaran_id='.$tahun_ajaran_id.' and F.id='.$kelas_tahun_ajaran_id.' and A.hari ='.$hari.' and ((('.$pelajaranKe.'  BETWEEN A.pelajaran_ke AND A.pelajaran_sampai) or ('.$pelajaranSampai.'  BETWEEN A.pelajaran_ke AND A.pelajaran_sampai)) or ((A.pelajaran_ke  BETWEEN '.$pelajaranKe.' AND '.$pelajaranSampai.')) or ((A.pelajaran_sampai  BETWEEN '.$pelajaranKe.' AND '.$pelajaranSampai.')))';
				$mJadwalPelajaran=$mcon->createCommand($sql1);		
				$rsDep=$mJadwalPelajaran->queryAll();
				$json = CJSON::encode($rsDep);				
				$arrrow = json_decode($json,true);
				$count =  count($arrrow);	$data_show="";
				if ($count>0)
				{
					for ($x=0; $x<count($arrrow); $x++)
						{
							$count2 = $count2 + $arrrow[$x]['count_status']; 
							if ($arrrow[$x]['count_status']==1) 
							{
								$data_show .= '\n'.$arrrow[$x]['karyawan_name'] .' ('.  $arrrow[$x]['kelas_name'] .') '. $arrrow[$x]['pelajaran_ke_name'] .' s/d '. $arrrow[$x]['pelajaran_sampai_name'];
							}
						}
						$count = $count2;
				}
				if ($count==0){
				$jadwalpelajaran = new MJadwalPelajaran;
				$jadwalpelajaran->karyawan_id = Yii::app()->request->getParam("karyawan_id","");	
//				$jadwalpelajaran->tahun_ajaran_id = $tahun_ajaran_id;
				$jadwalpelajaran->kelas_tahun_ajaran_id = $kelas_tahun_ajaran_id;
				$jadwalpelajaran->pelajaran_id =  $pelajaran_id;
				$jadwalpelajaran->hari = $hari;
				$jadwalpelajaran->pelajaran_ke = $pelajaranKe;
				$jadwalpelajaran->pelajaran_sampai = $pelajaranSampai;		
				$jadwalpelajaran->status = Yii::app()->request->getParam("status","");
				$jadwalpelajaran->keterangan = Yii::app()->request->getParam("keterangan","");				
				$jadwalpelajaran->is_active=$status;
				if ($jadwalpelajaran->save()){
					echo "<script>
						alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
						window.opener.location.reload();
						window.close();					
					</script>";
					exit;
				}else{
					print_r($jadwalpelajaran->getErrors());
				}		}
				else
				{	echo "<script>
						alert('".Yii::t("dialog","Ada kesalahan data...!".$data_show). "');					
						window.close();					
					</script>";}		
			}else if ($op == "edit"){
				LoginUser::getPermission("akademik_gdp_jadwalpelajaran_edit");
				//print_r ($_POST);
				$id = Yii::app()->request->getParam("id","");						
				$kelas_tahun_ajaran_id= Yii::app()->request->getParam("kelas_tahun_ajaran_id","");	
				$tahun_ajaran_id= Yii::app()->request->getParam("tahun_ajaran_id","");	
				$pelajaranKe=  Yii::app()->request->getParam("pelajaran_ke","");
				$pelajaranSampai= Yii::app()->request->getParam("pelajaran_sampai","");		
				$hari=Yii::app()->request->getParam("hari","");	
				$pelajaran_id = Yii::app()->request->getParam("pelajaran_id","");					
				$mcon= Yii::app()->db;
				$sql1='select A.id as id,F.tahun_ajaran_id,F.kelas_id,A.karyawan_id,A.pelajaran_id,A.status,A.hari,A.pelajaran_ke,A.pelajaran_sampai,A.is_active,A.keterangan, C.name as kelas_name, D.name as pelajaran_name , E.name as karyawan_name ,if (A.pelajaran_id='.$pelajaran_id.',0,1) as count_status ,(select name from m_jam_pelajaran where id=A.pelajaran_ke) as pelajaran_ke_name ,(select name from m_jam_pelajaran where id=A.pelajaran_sampai) as pelajaran_sampai_name from m_jadwal_pelajaran A left join m_kelas_tahun_ajaran F on A.kelas_tahun_ajaran_id=F.id left join m_kelas C on F.kelas_id=C.id left join m_pelajaran D on A.pelajaran_id=D.id left join m_karyawan E on A.karyawan_id=E.id  where F.tahun_ajaran_id='.$tahun_ajaran_id.' and F.id='.$kelas_tahun_ajaran_id.' and A.hari ='.$hari.'  and (('.$pelajaranSampai.'  BETWEEN A.pelajaran_ke AND A.pelajaran_sampai) or  (A.pelajaran_sampai  BETWEEN '.$pelajaranKe.' AND '.$pelajaranSampai.')) and A.id !='.$id;
				$mJadwalPelajaran=$mcon->createCommand($sql1);		
				$rsDep=$mJadwalPelajaran->queryAll();
				$json = CJSON::encode($rsDep);				
				$arrrow = json_decode($json,true);
				$count =  count($arrrow);	
				if ($count>0)
				{
					for ($x=0; $x<count($arrrow); $x++)
						{
							$count2 = $count2 + $arrrow[$x]['count_status']; 
							if ($arrrow[$x]['count_status']==1) 
							{
								$data_show .= '\n'.$arrrow[$x]['karyawan_name'] .' ('.  $arrrow[$x]['kelas_name'] .') '. $arrrow[$x]['pelajaran_ke_name'] .' s/d '. $arrrow[$x]['pelajaran_sampai_name'];
							}
						}
						$count = $count2;
				}							
				if ($count==0){				
				$jadwalpelajaran = MJadwalPelajaran::model()->find("id=:id",array(":id"=>$id));
				$jadwalpelajaran->karyawan_id = Yii::app()->request->getParam("karyawan_id","");	
//				$jadwalpelajaran->tahun_ajaran_id =$tahun_ajaran_id;
				$jadwalpelajaran->kelas_tahun_ajaran_id =$kelas_tahun_ajaran_id;
				$jadwalpelajaran->pelajaran_id = $pelajaran_id;
				$jadwalpelajaran->hari =$hari;
				$jadwalpelajaran->pelajaran_ke = $pelajaranKe;
				$jadwalpelajaran->pelajaran_sampai =$pelajaranSampai;			
				$jadwalpelajaran->status = Yii::app()->request->getParam("status","");
				$jadwalpelajaran->keterangan = Yii::app()->request->getParam("keterangan","");				
				$jadwalpelajaran->is_active=$status;
				if ($jadwalpelajaran->save()){
					echo "<script>
						alert('".Yii::t("dialog","Perubahan data sudah di simpan...!"). "');	
						window.opener.location.reload();
						window.close();					
					</script>";
					exit;
				}else{
					print_r($jadwalpelajaran->getErrors());
				}			
				}
				else
				{	echo "<script>
						alert('".Yii::t("dialog","Ada kesalahan data...!".$data_show). "');									
						window.close();					
					</script>";}	
			}else if ($op == "del"){
				if (!LoginUser::getPermission("akademik_gdp_jadwalpelajaran_del",null,false)){
					$this->result["actionStatus"] =1;
					$this->result["actionMessage"] = Yii::t("alert","Anda tidak memiliki ijin untuk menghapus data ini !");
					$this->result["actionError"] =array();
					$this->result["model"] = array();
					header('Content-type:application/json');
					echo CJSON::encode($this->result);
					return;
				}
				$id = Yii::app()->request->getParam("id","");
				$conn = Yii::app()->db;
				$sql = "delete from m_jadwal_pelajaran where id = :id";
				$command = $conn->createCommand($sql);
				try{
					//$command->bindParam(":id",$id,PDO::PARAM_INT);
					if ($command->execute(array(":id"=>$id))){
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="";
						$this->result["actionError"] =array();
						$this->result["model"] = array();
					}
				}catch(Exception $e){
					$this->result["actionStatus"] =2;
					$this->result["actionMessage"] = $e->getMessage();
					$this->result["actionError"] =array();
					$this->result["model"] = array();
				}
				Yii::app()->db->active = false;
 		
				//header('Content-type:application/json');
				//echo CJSON::encode($this->result);
			} 
				
			 
			
			
		}
		
		public function actionform(){
			$op = Yii::app()->request->getParam("op","add");
			$callback = Yii::app()->request->getParam("callback","");
			$department_id = Yii::app()->request->getParam("department_id","0");
			
			$data = array("op"=>"add","callback"=>"","id"=>0,"department_id" =>$department_id,"name"=>"","singkatan"=>"","keterangan"=>"","kelas_tahun_ajaran_id"=>"","is_active"=>"1");
			
			if ($op == "add"){
				LoginUser::getPermission("akademik_gdp_jadwalpelajaran_add");
				$data["op"] = "add";
				$data["callback"] = $callback;				
			}else if($op =="edit"){				
				LoginUser::getPermission("akademik_gdp_jadwalpelajaran_edit");			
				$data["op"] = "edit";
				$data["callback"] = $callback;				
				$id = Yii::app()->request->getParam("id","0");				
				$data["id"] = $id;
				
//				$rsJadwalPelajaran = MJadwalPelajaran::model()->findAll(array("condition"=>"id = $id","order"=>"id"));
				$mcon= Yii::app()->db;
				$sql1='select A.id as id ,F.tahun_ajaran_id,F.id as kelas_tahun_ajaran_id, F.kelas_id,A.karyawan_id,A.pelajaran_id,A.status,A.hari,A.pelajaran_ke,A.pelajaran_sampai,A.is_active,A.keterangan,B.tingkatan_id as tingkatan_id from  m_jadwal_pelajaran A left join m_kelas_tahun_ajaran F on A.kelas_tahun_ajaran_id=F.id left join m_kelas B on F.kelas_id=B.id   where A.id='.$id.' order by A.id';
				$mJadwalPelajaran=$mcon->createCommand($sql1);		
				$rsDep=$mJadwalPelajaran->queryAll();	
				$json = CJSON::encode($rsDep);
				$arrdata = json_decode($json,true);
				if (count($arrdata) > 0){
					for ($x=0; $x<count($arrdata); $x++)
					{				
						$data["tahun_ajaran_id"] =$arrdata[$x]['tahun_ajaran_id'];	
						$data["kelas_tahun_ajaran_id"] = $arrdata[$x]['kelas_tahun_ajaran_id'];									
						$data["karyawan_id"] = $arrdata[$x]['karyawan_id'];	
						$data["pelajaran_id"] =$arrdata[$x]['pelajaran_id'];	
						$data["status"] = $arrdata[$x]['status'];			
						$data["hari"] = $arrdata[$x]['hari'];	
						$data["pelajaran_ke"] = $arrdata[$x]['pelajaran_ke'];	
						$data["pelajaran_sampai"] = $arrdata[$x]['pelajaran_sampai'];											
						$data["tingkatan_id"] = $arrdata[$x]['tingkatan_id'];			
						$data["keterangan"] = $arrdata[$x]['keterangan'];																																				
						$data["is_active"] =$arrdata[$x]['is_active'];	
					}
				}				
			}			
			$this->layout ="//layouts/ly_blank";
			$this->render("form",$data);
		}
				
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
	public function actionPopupWindow()
	{
		$this->layout = "//layouts/ly_blank";
		$this->render('popupWindow');
	}		
		
	}
?>